package com.library.uiframework.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.library.uiframework.slidingclose.SlideHelperSlidingCloseInterface;
import com.library.uiframework.slidingclose.SlideHelperSlidingScrollInterface;
import com.library.uiframework.slidingclose.SlidingClosableRelativeLayout;
import com.library.uiframework.slidingclose.SlidingCloseableHelper;

/**
 * @author hongkai.qian
 * @version 1.0.0
 * @since 15-11-19
 */
public abstract class SlideCloseFragment extends BaseFragment
        implements SlideHelperSlidingCloseInterface, SlideHelperSlidingScrollInterface {

    private SlidingCloseableHelper mSlidingCloseableHelper = new SlidingCloseableHelper();

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = buildContentView(inflater, container, savedInstanceState);
        setBusinessView(contentView);
        View view = mSlidingCloseableHelper.injectView(contentView);
        return view;
    }

    View buildContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return onCreateContentView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSlidingCloseableHelper.setSlideHelperSlidingCloseInterface(this);
        mSlidingCloseableHelper.setSlideHelperSlidingScrollInterface(this);
    }

    protected abstract View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    @Override
    public void onSlidingClosed() {
        finish();
    }

    @Override
    public void onScrollStarted() {
        getFragmentBackHelper().showPreviousFragment();
    }

    @Override
    public void onScrollEnded(boolean isCloseState) {
        getFragmentBackHelper().hidePreviousFragment();
    }

    /**
     * @return SlidingClosableRelativeLayout
     */
    public SlidingClosableRelativeLayout getSlidingContainer() {
        if (mSlidingCloseableHelper != null) {
            return mSlidingCloseableHelper.getSlidingClosableRelativeLayout();
        }
        return null;
    }

    /**
     * 设置滑动关闭的模式
     *
     * @param closeMode 滑动关闭的模式
     */
    public void setSlidingCloseMode(int closeMode) {
        if (mSlidingCloseableHelper != null && mSlidingCloseableHelper.getSlidingClosableRelativeLayout() != null) {
            mSlidingCloseableHelper.getSlidingClosableRelativeLayout().setSlidingCloseMode(closeMode);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mSlidingCloseableHelper != null && mSlidingCloseableHelper.getSlidingClosableRelativeLayout() != null) {
            mSlidingCloseableHelper.getSlidingClosableRelativeLayout().setVisibility(View.GONE);
            mSlidingCloseableHelper.getSlidingClosableRelativeLayout().setOnSlidingCloseListener(null);
            mSlidingCloseableHelper.getSlidingClosableRelativeLayout().setOnSlidingScrollListener(null);
            mSlidingCloseableHelper = null;
        }
    }
}
